package com.sharemob.tinchat.modules.home;


import java.net.URL;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sharemob.tinchat.R;
import com.sharemob.tinchat.base.BaseFragment;
import com.sharemob.tinchat.base.CircleImageView;
import com.sharemob.tinchat.base.Item;
import com.sharemob.tinchat.base.NoScrollListview;
import com.sharemob.tinchat.lib.Utils;
import com.sharemob.tinchat.lib.common.SMGlobal;
import com.sharemob.tinchat.modules.account.ActivityAccountInfo;
import com.sharemob.tinchat.modules.account.ActivityFeedback;
import com.sharemob.tinchat.modules.account.ActivityPrivacy;
import com.sharemob.tinchat.modules.account.ActivitySetting;
import com.sharemob.tinchat.modules.account.ActivityShare;
import com.sharemob.tinchat.modules.account.MyspaceActivity;
import com.sharemob.tinchat.modules.dao.cache.CacheManager;
import com.tencent.mm.sdk.platformtools.BackwardSupportUtil.BitmapFactory;

@SuppressLint("ResourceAsColor")
public class FragmentCenter extends BaseFragment{

    private NoScrollListview listView=null;
    private NoScrollListview space_listView=null;
    private String[] names=new String[]{"分享","意见反馈","隐私设置","更多"};
    private String[] tags=new String[]{"share","feedback","setting","more"};
    private int[] resids=new int[]{R.drawable.icon_account_share,R.drawable.icon_account_feedback,
    				R.drawable.icon_account_notification,R.drawable.icon_account_more};
    
    private ArrayList<Item> list = new  ArrayList< Item>(); 
    private ArrayList<Item> space_list = new  ArrayList< Item>(); 
    
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
			
    
    
    }
    
    private void initListDapter(){
    	//玩家个人信息
    	CircleImageView account_header=(CircleImageView)this.getActivity().findViewById(R.id.account_header);
   	 	Utils.StrictMode();
    	URL url=null;
		try {
			url = new URL(CacheManager.getInstance().getAccount().getHeadiconurl());
			Bitmap pngBM = BitmapFactory.decodeStream(url.openStream()); 
			account_header.setImageBitmap(pngBM);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
   	 	TextView tv_sex=(TextView)this.getActivity().findViewById(R.id.account_info_sex_age);
	 
//		int age=CacheManager.getInstance().getAccount().getAge().intValue();
   	 	int age=10;
   	 	int sex=1;
//		int sex=CacheManager.getInstance().getAccount().getSex();
		if(sex==1){
			tv_sex.setText(String.valueOf(age));
			tv_sex.setBackgroundResource(R.drawable.style_bg_sex_male);
			tv_sex.setCompoundDrawables(SMGlobal.getInstance().getDrawableByResID(R.drawable.ic_icon_male),null, null, null);
		}else if(sex==2){
			tv_sex.setText(String.valueOf(age));
			tv_sex.setBackgroundResource(R.drawable.style_bg_sex_female);
		}
    	
		TextView tv_city=(TextView)this.getActivity().findViewById(R.id.account_info_city);
		tv_city.setText(CacheManager.getInstance().getAccount().getCity());
		
		TextView tv_nickname=(TextView)this.getActivity().findViewById(R.id.account_info_nickname);
		tv_nickname.setText(CacheManager.getInstance().getAccount().getNickname());
    	
    	//--------空间-----------
    	Item space=new Item();
    	space.setId("space");
    	space.setIcon(true);
    	space.setResid(R.drawable.icon_account_room);
    	space.setName("空间"); 
   	 	space_list.add(space);
   	 	//--------游戏------------
//    	Item game=new Item();
//    	game.setId("game");
//    	game.setIcon(true);
//    	game.setResid(R.drawable.icon_account_dowmload);
//    	game.setName("游戏"); 
//   	space_list.add(game);

   	 	space_listView= getInitListView(space_list,R.id.listView_account_space);
    	
    	//--------------设置------------
        for(int i=0;i<names.length;i++){
        	 Item item=new Item();
        	 item.setId(tags[i]);
        	 item.setIcon(true);
        	 item.setResid(resids[i]);
        	 item.setName(names[i]); 
             list.add(item);
        }
        listView= getInitListView(list,R.id.listView_account_setting);
        
        getView().findViewById(R.id.layout_account_info).setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            { 
            	Intent intent = new Intent(); 
                intent.setClass(FragmentCenter.this.getActivity(),ActivityAccountInfo.class);
                startActivity(intent);
            	getActivity().overridePendingTransition(R.anim.activity_open,0);
            }
        });
        
        
    }
    
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		
		return inflater.inflate(R.layout.fragment_user_tab, container,true);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		 initListDapter();
	
	}

	
	public void spaceListEvent(int position){
		 Item item=space_list.get(position);
		 System.out.println(item.getId());
		 if("space".equals(item.getId())){
			  Intent intent = new Intent(); 
              intent.setClass(FragmentCenter.this.getActivity(),MyspaceActivity.class);
              startActivity(intent);
              getActivity().overridePendingTransition(R.anim.activity_open,0);
		 }
//		 else if("game".equals(item.getId())){
//			  Intent intent = new Intent(); 
//              intent.setClass(FragmentCenter.this.getActivity(),GameActivity.class);
//              startActivity(intent);
//              getActivity().overridePendingTransition(R.anim.activity_open,0);
//			 
//		 }
	}
	public void settingListEvent(int position){
		 Item item=list.get(position);
         if("share".equals(item.getId())){
        	 ActivityShare mMoreWindow = new ActivityShare(this.getActivity());
        	 mMoreWindow.init();

    		mMoreWindow.showMoreWindow(this.getView(),100);
//        	 gotoView(FragmentCenter.this.getActivity(), ActivityShare.class);
          }else if("feedback".equals(item.getId())){
        	  
            gotoView(FragmentCenter.this.getActivity(), ActivityFeedback.class);
          }else if("setting".equals(item.getId())){
          	
              gotoView(FragmentCenter.this.getActivity(), ActivityPrivacy.class);
          }else if("more".equals(item.getId())){
        	  Intent intent = new Intent(); 
              intent.setClass(FragmentCenter.this.getActivity(),ActivitySetting.class);
              startActivity(intent);
              getActivity().overridePendingTransition(R.anim.in_from_right,0);
          }
	}
	protected void keyEventOfListView(int position, int id) {

		switch (id) {
		case R.id.listView_account_space:
			spaceListEvent(position);
			break;
		case R.id.listView_account_setting:
			settingListEvent(position);
			break;
		default:
			break;
		}
		

         
	}
}
